﻿using System;
using System.Collections.Generic;
using System.Linq;
using CfgMusic;
using CSUtils;
using G02.PlayerData;
using G02.SceneConfig;
using TripleJumpRoleCfg;
using U3dGame;
using U3dUtils;
using UI00;
using UnityEngine;
using UnityEngine.UI;
namespace G02.TripleJump.UI {
    public class UI_NpcPreview : UIBase {
        public static string prefabPath;

        protected override string _GetUIPrefabPath() {
            if (prefabPath != null) {
                return prefabPath;
            }
            return "TripleJump/UI/UI_NpcPreview/UI_NpcPreview.prefab";
        }

        public static Transform parent;

        protected override Transform _GetUIParent() {
            if (parent != null) {
                return parent;
            }
            return UIBase.uiLayer4_uiFront;
        }

        public static void Open(bool issss) {
            UIManage.Open<UI_NpcPreview>();
        }

        RectTransform cardPrefab;

        C_NpcInfoView npcInfoView = new C_NpcInfoView();
        Button closeButton;

        protected override void _Init() {
            cardPrefab = GetControl<RectTransform>("view/selectBox/bottom/cardPrefab");
            closeButton = GetControl<Button>("view/closeButton");
            npcInfoView._Init(GetControl<RectTransform>("npcInfoView"), this);
            m_rootRt.GetComponent<Button>().
                onClick.AddListener(delegate() {
                    CMusicManage.PlayMusic(MusicCfg.button_negative);
                    Close();
                });
            closeButton.onClick.AddListener(delegate() {
                CMusicManage.PlayMusic(MusicCfg.button_negative);
                Close();
            });

            _InitData();

            var cfgIdArr = CfgTJumpRole.arr;
            _InitSelectBox(cfgIdArr, cardPrefab, null);
        }

        protected override void _Open(Dictionary<string, object> paraDic) {
        }

        Dictionary<int, C_Card> cardDic = new Dictionary<int, C_Card>();

        /// <summary>
        /// 初始化选择框
        /// </summary>
        void _InitSelectBox(CfgTJumpRole[] cfgs, RectTransform cardPrefab, Action<int> callback) {
            cardPrefab.gameObject.SetActive(false);
            RectTransform bottom = (RectTransform)cardPrefab.parent;
            RectTransform selectBox = (RectTransform)bottom.parent;
            int numberX = (int)(bottom.rect.width / cardPrefab.rect.width);
            for (int i = 0; i < cfgs.Length; i++) {
                RectTransform tt = RectTransform.Instantiate(cardPrefab);
                tt.gameObject.SetActive(true);
                tt.SetParent(cardPrefab.transform.parent);
                tt.localScale = new Vector3(1, 1, 1);
                tt.anchorMin = new Vector2(0, 1);
                tt.anchorMax = new Vector2(0, 1);
                tt.localPosition = cardPrefab.localPosition + new Vector3((cardPrefab.rect.width + 1) * (i % numberX), -(cardPrefab.rect.height + 1) * (i / numberX), 0);
                //gg.GetComponent<Image>().overrideSprite = imageArr[i];
                C_Card nn = new C_Card();
                cardDic[cfgs[i].id] = nn;
                nn._Init(tt);
                nn.name.text = cfgs[i].name;
                // nn.image.sprite = C_UIManage.GetInstance.sprite_fish.GetSprite(cfgs[i].onlyAssetPath);
                int ii = i;
                tt.GetComponent<Button>().
                    onClick.AddListener(delegate() {
                        //if (callback != null) {
                        //    callback(ii);
                        //} else {
                        //    Debug.LogErrorFormat("卡片回调事件==null");
                        //}
                        CMusicManage.PlayMusic(MusicCfg.button_positive);
                        npcInfoView.Open(tt, cfgs, ii, true);
                    });
            }
            float high = (cardPrefab.rect.height + 1) * ((cfgs.Length + numberX - 1) / numberX);
            if (selectBox.sizeDelta.y >= high) {
                high = selectBox.sizeDelta.y;
            }
            bottom.sizeDelta = new Vector2(bottom.sizeDelta.x, high);
            bottom.localPosition = Vector3.zero;
        }

        class C_Card {
            public Text name;
            public Image image;

            public void _Init(RectTransform root) {
                name = root.Find("name").GetComponent<Text>();
                image = root.Find("image").GetComponent<Image>();
            }
        }

        Dictionary<int, string> monsterLanguageDic = new Dictionary<int, string>();

        Dictionary<string, int> _datadic = new Dictionary<string, int>();

        public void _InitData() {
            var dic = CfgTJumpRole.arr;
//            while (dic.MoveNext()) {
//                CInt4 onlyPath = CInt4.Get(dic.Current.Value.onlyAssetPath);
//                _datadic[onlyPath] = new LongData("怪物收集成就金币奖励_" + onlyPath, 0);
//            }
        }

        public class C_NpcInfoView {
            RectTransform root;
            Text[] attributeTexts = new Text[15];
            Image image;
            Text name;
            Button closeButton, LeftButton, rightButton;

            C_CollectAchievementValue collectAchievementValue = new C_CollectAchievementValue();
            UI_NpcPreview emplayer;

            public void _Init(RectTransform root, UI_NpcPreview emplayer) {
                this.root = root;
                this.emplayer = emplayer;
                root.gameObject.SetActive(false);
                image = emplayer.GetControl<Image>("view/cardPrefab/image", root);
                name = emplayer.GetControl<Text>("view/cardPrefab/name", root);
                closeButton = emplayer.GetControl<Button>("view/closeButton", root);
                LeftButton = emplayer.GetControl<Button>("view/LeftButton", root);
                rightButton = emplayer.GetControl<Button>("view/rightButton", root);
                collectAchievementValue._Init(emplayer.GetControl<RectTransform>("view/collectAchievementValue", root), emplayer);

                Text attributeText = emplayer.GetControl<Text>("view/attribute/text", root);
                RectTransform attributeText0 = attributeText.GetComponent<RectTransform>();
                attributeText.gameObject.SetActive(false);
                for (int i = 0; i < attributeTexts.Length; i++) {
                    attributeTexts[i] = Text.Instantiate(attributeText);
                    attributeTexts[i].transform.SetParent(attributeText0.parent);
                    attributeTexts[i].transform.localPosition = new Vector3(attributeText0.localPosition.x, -30 - attributeText0.sizeDelta.y * i, 0);
                }
                root.GetComponent<Button>().
                    onClick.AddListener(delegate() {
                        CMusicManage.PlayMusic(MusicCfg.button_negative);
                        Close();
                    });
                closeButton.onClick.AddListener(delegate() {
                    CMusicManage.PlayMusic(MusicCfg.button_negative);
                    Close();
                });
                LeftButton.onClick.AddListener(delegate() {
                    CMusicManage.PlayMusic(MusicCfg.button_negative);
                    cfgIndex--;
                    cfgIndex = cfgIndex < 0 ? cfg.Length - 1 : cfgIndex;
                    Open(parentButton, cfg, cfgIndex, false);
                });
                rightButton.onClick.AddListener(delegate() {
                    CMusicManage.PlayMusic(MusicCfg.button_negative);
                    cfgIndex++;
                    cfgIndex = cfgIndex % cfg.Length;
                    Open(parentButton, cfg, cfgIndex, false);
                });
            }

            Transform parentButton;
            CfgTJumpRole[] cfg;
            int cfgIndex;
            bool isUIAnimction;

            public void Open(Transform parentButton, CfgTJumpRole[] cfg, int cfgIndex, bool isUIAnimction = false) {
                this.parentButton = parentButton;
                this.cfg = cfg;
                this.cfgIndex = cfgIndex;
                this.isUIAnimction = isUIAnimction;
                S_ShowInfo(cfg[cfgIndex]);
                collectAchievementValue.S_RefreshData(cfg[cfgIndex].artPath);
                if (isUIAnimction) {
                    root.AnimOpenView(parentButton);
                } else {
                    root.gameObject.SetActive(true);
                }
            }

            public void Close() {
                if (isUIAnimction) {
                    root.AnimCloseView(parentButton);
                } else {
                    root.gameObject.SetActive(false);
                }
            }

            // G01.UILanguage._UI_NpcPreview npcPreview00;
            void S_ShowInfo(CfgTJumpRole cfg) {
                //npcPreview00 = C_UIManage.uILanguage.UI_NpcPreview;
                //List<string> list = new List<string>();
                //if (cfg.SlowSpeedPlayerStartDistance > 0) {
                //    list.Add(string.Format(npcPreview00.o_SlowSpeedPlayer, cfg.SlowSpeedPlayerStartDistance));
                //}
                //if (cfg.deathSplit > 0) {
                //    list.Add(string.Format(npcPreview00.o_deathSplit, cfg.deathSplit));
                //}
                //if (cfg.specialMotionType > 0) {
                //    switch (cfg.specialMotionType) {
                //        case 1:
                //            list.Add(string.Format(npcPreview00.o_specialMotionType1, cfg.specialMotionValue[0]));
                //            break;
                //        case 2:
                //            list.Add(string.Format(npcPreview00.o_specialMotionType2, cfg.specialMotionValue[0]));
                //            break;
                //        case 3:
                //            list.Add(string.Format(npcPreview00.o_specialMotionType3, cfg.specialMotionValue[0]));
                //            break;
                //        case 4:
                //            list.Add(string.Format(npcPreview00.o_specialMotionType4, cfg.specialMotionValue[0]));
                //            break;
                //    }
                //}
                //if (cfg.attackDropGoldCoinRatio > 0) {
                //    list.Add(string.Format(npcPreview00.o_attackDropGoldCoinRatio, cfg.attackDropGoldCoinRatio));
                //}
                //if (cfg.fiveAngleFixed > 0) {
                //    list.Add(string.Format(npcPreview00.o_fiveAngleFixed, cfg.fiveAngleFixed));
                //}
                //if (cfg.defense.Length!=1) {
                //    list.Add(string.Format(npcPreview00.o_defense, cfg.defense[0]));
                //}
                //if (cfg.repel.Length != 1) {
                //    list.Add(string.Format(npcPreview00.o_repel, cfg.repel[0]));
                //}
                //if (cfg.bloodTransfusion.Length != 1) {
                //    list.Add(string.Format(npcPreview00.o_bloodTransfusion, cfg.bloodTransfusion[0], cfg.bloodTransfusion[1]));
                //}
                ////if (cfg.breathing.Length != 1) {
                ////    list.Add(string.Format(npcPreview00.o_bloodTransfusion, cfg.breathing.ToStringLog()));
                ////}
                //image.sprite = C_UIManage.GetInstance.sprite_fish.GetSprite(cfg.onlyAssetPath);
                //name.text = emplayer.monsterLanguageDic[cfg.id];
                //for (int i = 0; i < attributeTexts.Length; i++) {
                //    if(i< list.Count ) {
                //        attributeTexts[i].text = list[i];
                //        attributeTexts[i].gameObject.SetActive(true);
                //    } else {
                //        attributeTexts[i].gameObject.SetActive(false);
                //    }
                //}
            }

            public class C_CollectAchievementValue {
                Button rewardButton;
                Text rewardNumber, number, title;
                Image rewardImage;
                Scrollbar scrollbar;
                UI_NpcPreview emplayer;

                public void _Init(RectTransform root, UI_NpcPreview emplayer) {
                    this.emplayer = emplayer;
                    title = emplayer.GetControl<Text>("title", root);
                    scrollbar = emplayer.GetControl<Scrollbar>("Scrollbar", root);
                    number = emplayer.GetControl<Text>("Scrollbar/number", root);
                    rewardButton = emplayer.GetControl<Button>("rewardButton", root);
                    rewardImage = emplayer.GetControl<Image>("rewardButton", root);
                    rewardNumber = emplayer.GetControl<Text>("rewardButton/rewardNumber", root);
                    rewardButton.onClick.AddListener(delegate() {
                        if (isClick) {
                            CMusicManage.PlayMusic(MusicCfg.button_negative);
                            emplayer._datadic[onlyPath]++;
                            S_RefreshData(onlyPath);
                            //UI_Main.S_AddDiamonds("z收集鱼类成就", false, rewardDiamond, rewardButton.transform);
                        }
                    });
                }

                bool isClick;
                string onlyPath;
                int rewardDiamond;

                public void S_RefreshData(string onlyPath) {
                    this.onlyPath = onlyPath;
                    int clickCount = emplayer._datadic[onlyPath];
                    //long currentValue = C_DataManage.GetInstance.collectAchievementValue.S_GetMonsterCollectAchievementValue(onlyPath);
                    //int[] achievementValue = Cfg_Constant.o_default.fishCollectAchievementValue;
                    //int[] rewardDiamonds = Cfg_Constant.o_default.fishCollectAchievementRewardDiamonds;
                    //rewardDiamond = rewardDiamonds[clickCount];
                    //if (achievementValue[clickCount]< currentValue) {
                    //    rewardImage.color = Color.blue.S_SetColor("00A109");
                    //    isClick = true;
                    //} else {
                    //    rewardImage.color = Color.blue.S_SetColor("444444");
                    //    isClick = false;
                    //}
                    //rewardNumber.text ="+"+ rewardDiamond;
                    //scrollbar.size = (currentValue*1f) / achievementValue[clickCount];
                    //long currentValue0 = currentValue > achievementValue[clickCount] ? achievementValue[clickCount] : currentValue;
                    //number.text = string.Format("{0}/{1}", currentValue0, achievementValue[clickCount]);
                    //title.text = emplayer.npcInfoView.npcPreview00.o_collectNumber +"  "+ currentValue;
                }
            }
        }
    }
}
